package com.jichangxiu.server.platform.action;

import cn.hutool.core.collection.ListUtil;
import com.jichangxiu.common.annotation.*;
import com.jichangxiu.common.entity.bo.PageData;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.framework.action.BaseAction;
import com.jichangxiu.server.platform.entity.vo.PlatformMenuAddVo;
import com.jichangxiu.server.platform.entity.vo.PlatformMenuEditVo;
import com.jichangxiu.server.platform.entity.vo.PlatformMenuQueryVo;
import com.jichangxiu.server.platform.service.PlatformMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * PlatformMenuAction:
 * 平台-菜单管理控制器
 *
 * @author Jcx
 * @create 2023-02-25 22:10
 */
@Slf4j
@Rest("/platform/menu")
@Tag(name = "平台-菜单管理控制器")
public class PlatformMenuAction extends BaseAction {

    @Resource
    private PlatformMenuService platformMenuService;

    /**
     * 新增菜单
     *
     * @param platformMenuAddVo 菜单视图对象
     * @return 是否成功
     */
    @Post("/add")
    @Operation(summary = "平台-新增菜单")
    @PreAuthorize("@ss.hasPermission('platform:menu:add')")
    public String add(@RequestBody PlatformMenuAddVo platformMenuAddVo) {
        return platformMenuService.add(platformMenuAddVo);
    }

    /**
     * 多选级联删除菜单
     *
     * @param menuIds 菜单ID列表
     * @return 是否成功
     */
    @Delete("/del")
    @Operation(summary = "平台-多选级联删除菜单")
    @PreAuthorize("@ss.hasPermission('platform:menu:del')")
    public String del(@RequestBody String[] menuIds) {
        return toStr(platformMenuService.del(ListUtil.toList(menuIds)));
    }

    /**
     * 修改菜单信息
     *
     * @param platformMenuEditVo 菜单视图对象
     * @return 是否成功
     */
    @Put("/edit")
    @Operation(summary = "平台-修改菜单信息")
    @PreAuthorize("@ss.hasPermission('platform:menu:edit')")
    public String edit(@RequestBody PlatformMenuEditVo platformMenuEditVo) {
        return toStr(platformMenuService.edit(platformMenuEditVo));
    }

    /**
     * 根据ID获取树形菜单详情
     *
     * @param menuId 菜单ID
     * @return 树形菜单详情
     */
    @Get("/info/{menuId}")
    @Operation(summary = "平台-根据ID获取菜单详情")
    @PreAuthorize("@ss.hasPermission('platform:menu:info')")
    public Record info(@PathVariable("menuId") @NotBlank(message = "菜单ID不能为空") String menuId) {
        return platformMenuService.info(menuId);
    }

    /**
     * 根据用户ID获取该用户所拥有的树形菜单列表
     *
     * @param userId 用户ID
     * @return 树形菜单列表
     */
    @Get(value = "/getMenuListByUser/{userId}", logEnabled = false)
    @Operation(summary = "平台-根据用户ID获取菜单")
    @PreAuthorize("isAuthenticated()")
    public List<Record> getMenuListByUser(@PathVariable("userId") @NotBlank(message = "用户ID不能为空") String userId) {
        return platformMenuService.getMenuListByUser(userId);
    }

    /**
     * 分页查询树形菜单列表
     *
     * @param platformMenuQueryVo 菜单视图对象
     * @return 分页树形菜单列表
     */
    @Get("/pageList")
    @Operation(summary = "平台-分页查询树形菜单列表")
    @PreAuthorize("@ss.hasPermission('platform:menu:pageList')")
    public PageData<Record> pageList(@ParameterObject PlatformMenuQueryVo platformMenuQueryVo) {
        return platformMenuService.pageList(platformMenuQueryVo);
    }

}
